package com.yiwyn;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;

public class WordCountBatchDemo {
    public static void main(String[] args) throws Exception {
        // 创建执行环境
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        // 读取数据
        DataSource<String> ds = env.readTextFile("/Users/yiwyn/BigdataProject/bigdata-demo/flink-demo/input/test.txt");
        // 切分 转换
        FlatMapOperator<String, Tuple2<String, Integer>> wordAndOne = ds.flatMap((FlatMapFunction<String, Tuple2<String, Integer>>) (value, collector) -> {
            String[] s = value.split(" ");
            for (String word : s) {
                // 转换完成
                Tuple2<String, Integer> t = Tuple2.of(word, 1);
                // 采集器采集
                collector.collect(t);
            }
        }).returns(Types.TUPLE(Types.STRING, Types.INT));
        // 按照单词分组
        // 分组聚合
        AggregateOperator<Tuple2<String, Integer>> sum = wordAndOne.groupBy(0).sum(1);
        // 输出
        sum.print();
    }
}
